// Decompiled by Jad v1.5.7g. Copyright 2000 Pavel Kouznetsov.
// Jad home page: http://www.geocities.com/SiliconValley/Bridge/8617/jad.html
// Decompiler options: packimports(3) fieldsfirst ansi 
// Source File Name:   DocProcess.java

package com.oa.doc;

import com.oa.database.DataConn;
import com.oa.share.CheckPartner;
import com.oa.util.Format;
import java.io.PrintStream;
import java.sql.*;
import java.util.ArrayList;
import java.util.Collection;

// Referenced classes of package com.oa.doc:
//            DocInfo, DocLBInfo, DocWDInfo

public class DocProcess
{

    private DataConn dataConn;
    private Connection conn;
    private Statement stmt;
    private ResultSet rs;
    private String sSQL;
    private ArrayList arrayList;
    private ArrayList lbArrayList;
    private ArrayList wdArrayList;
    private Format format;
    private DocLBInfo docLBInfo;
    private int iKZBZ;
    private int iZWBZ;
    private String cTable;
    private String cField;
    private String dTable;
    private String mcField;
    private String idField;
    private int iRecordCount;
    private int iCurrentPage;
    private int iPageCount;
    private int iPageSize;
    private String sSearchField;
    private String sSearchKey;
    private String sSortField;
    private String sOrder;
    private static boolean strVERSION_F = false;
    private static String strVERSION_M = "doc==OAVER20050609";

    public DocProcess()
    {
        dataConn = null;
        conn = null;
        stmt = null;
        rs = null;
        sSQL = "";
        arrayList = null;
        lbArrayList = null;
        wdArrayList = null;
        format = new Format();
        docLBInfo = null;
        iKZBZ = 0;
        iZWBZ = 0;
        cTable = "";
        cField = "";
        dTable = "";
        mcField = "";
        idField = "";
        iRecordCount = 0;
        iCurrentPage = 0;
        iPageCount = 0;
        iPageSize = 0;
        sSearchField = null;
        sSearchKey = null;
        sSortField = null;
        sOrder = null;
        dataConn = new DataConn();
        conn = dataConn.getConnection();
    }

    public static String getVERSION_M()
    {
        return strVERSION_M;
    }

    public void close()
    {
        if(dataConn != null)
            dataConn.close();
    }

    public int getICurrentPage()
    {
        return iCurrentPage;
    }

    public int getIPageCount()
    {
        return iPageCount;
    }

    public int getIPageSize()
    {
        return iPageSize;
    }

    public int getIRecordCount()
    {
        return iRecordCount;
    }

    public void setIRecordCount(int iRecordCount)
    {
        this.iRecordCount = iRecordCount;
    }

    public void setIPageSize(int iPageSize)
    {
        this.iPageSize = iPageSize;
    }

    public void setIPageCount(int iPageCount)
    {
        this.iPageCount = iPageCount;
    }

    public void setICurrentPage(int iCurrentPage)
    {
        this.iCurrentPage = iCurrentPage;
    }

    public void setSearchField(String sSearchField)
    {
        this.sSearchField = sSearchField;
    }

    public void setSearchKey(String sSearchKey)
    {
        this.sSearchKey = sSearchKey;
    }

    public void setSortField(String sSortField)
    {
        this.sSortField = sSortField;
    }

    public void setOrder(int iOrder)
    {
        if(1 == iOrder)
            sOrder = "desc";
        else
        if(iOrder == 0)
            sOrder = "asc";
    }

    public Collection getArrayList()
    {
        return arrayList;
    }

    public Collection getLBArrayList()
    {
        return lbArrayList;
    }

    public Collection getWDArrayList()
    {
        return wdArrayList;
    }

    public Collection getList(int iCurrentPage, int iPageSize)
        throws Exception
    {
        try
        {
            this.iCurrentPage = iCurrentPage;
            iPageCount = iPageSize;
            int i = 0;
            int j = 0;
            int iPageEnd = 0;
            i = (iCurrentPage - 1) * iPageSize + 1;
            iPageEnd = (i + iPageSize) - 1;
            DocInfo docInfo = null;
            stmt = conn.createStatement(1004, 1008);
            arrayList = new ArrayList();
            sSQL = " IF (EXISTS(select name from sysobjects where name='ttt'))  drop table ttt ";
            try
            {
                stmt.executeUpdate(sSQL);
            }
            catch(Exception exception) { }
            sSQL = " CREATE TABLE TTT (  ID INT,  KZID INT,  QXID INT,  YGID INT,  KZBZ SMALLINT,  ZWBZ SMALLINT,  QXMC NVARCHAR(20),  KZMC NVARCHAR(100),  OBMC NVARCHAR(40)  ) ";
            stmt.executeUpdate(sSQL);
            for(iKZBZ = 0; iKZBZ < 2; iKZBZ++)
            {
                if(iKZBZ == 0)
                {
                    cTable = "OA_WDLB";
                    cField = "LBMC";
                } else
                if(iKZBZ == 1)
                {
                    cTable = "OA_WDXX";
                    cField = "WDBT";
                }
                for(iZWBZ = 0; iZWBZ < 3; iZWBZ++)
                {
                    if(iZWBZ == 0)
                    {
                        dTable = "OA_YGB";
                        idField = "YGID";
                        mcField = "YGXM";
                    } else
                    if(iZWBZ == 1)
                    {
                        dTable = "OA_ZWB";
                        idField = "ZWID";
                        mcField = "ZWMC";
                    } else
                    if(iZWBZ == 2)
                    {
                        dTable = "OA_BMB";
                        idField = "BMID";
                        mcField = "BMMC";
                    }
                    sSQL = "insert into ttt select a.ID,a.KZID,a.QXID,a.YGID,a.KZBZ,a.ZWBZ,b.QXMC,c." + cField + " as KZMC,d." + mcField + " as OBMC from OA_QXYG a,OA_QXXX b," + cTable + " c," + dTable + " d where a.QXID = b.ID and a.KZID = c.ID and a.YGID = d." + idField + " and a.KZBZ = " + iKZBZ + " and a.ZWBZ = " + iZWBZ;
                    stmt.executeUpdate(sSQL);
                }

            }

            sSQL = "select ID,KZID,QXID,YGID,KZBZ,ZWBZ,KZMC,QXMC,OBMC from ttt ";
            if(sSearchField != null && !"".equals(sSearchField) && !"".equals(sSearchKey) && sSearchKey != null)
                sSQL += " where " + sSearchField + " like '%" + sSearchKey + "%'";
            if(sSortField != null && !"".equals(sSortField) && !"".equals(sOrder) && sOrder != null)
                sSQL += " order by " + sSortField + " " + sOrder;
            else
                sSQL += "order by KZBZ,KZID,ZWBZ,YGID,QXID";
            for(rs = stmt.executeQuery(sSQL); rs.next() && j < iPageEnd;)
                if(j < i - 1)
                {
                    j++;
                } else
                {
                    docInfo = new DocInfo();
                    docInfo.setID(rs.getInt("ID"));
                    docInfo.setKZID(rs.getInt("KZID"));
                    docInfo.setQXID(rs.getInt("QXID"));
                    docInfo.setYGID(rs.getInt("YGID"));
                    docInfo.setKZBZ(rs.getInt("KZBZ"));
                    docInfo.setZWBZ(rs.getInt("ZWBZ"));
                    docInfo.setKZMC(format.toSpaceStr(rs.getString("KZMC")));
                    docInfo.setQXMC(format.toSpaceStr(rs.getString("QXMC")));
                    docInfo.setOBMC(format.toSpaceStr(rs.getString("OBMC")));
                    arrayList.add(docInfo);
                    j++;
                }

            rs.last();
            iRecordCount = rs.getRow();
            iPageCount = (iRecordCount - 1) / iPageSize + 1;
            rs.close();
            sSQL = " IF (EXISTS(select name from sysobjects where name='ttt'))  drop table ttt ";
            try
            {
                stmt.executeUpdate(sSQL);
            }
            catch(Exception exception1) { }
            stmt.executeUpdate(sSQL);
            stmt.close();
        }
        catch(Exception e)
        {
            System.err.println(e.getMessage());
            throw e;
        }
        return arrayList;
    }

    public Collection getDocLBList()
        throws Exception
    {
        try
        {
            stmt = conn.createStatement();
            lbArrayList = new ArrayList();
            DocLBInfo docLBInfo = new DocLBInfo();
            sSQL = "select ID,LBMC,FID,LAYER,LBBH from OA_WDLB order by SORT ";
            for(rs = stmt.executeQuery(sSQL); rs.next(); lbArrayList.add(docLBInfo))
            {
                docLBInfo = new DocLBInfo();
                docLBInfo.setID(rs.getInt("ID"));
                docLBInfo.setLBMC(format.toSpaceStr(rs.getString("LBMC")));
                docLBInfo.setFID(rs.getInt("FID"));
                docLBInfo.setLAYER(rs.getInt("LAYER"));
                docLBInfo.setLBBH(rs.getString("LBBH"));
            }

            rs.close();
            stmt.close();
        }
        catch(Exception e)
        {
            System.err.println(e.getMessage());
            throw e;
        }
        return lbArrayList;
    }

    public Collection getDocLBList(String sCond)
        throws Exception
    {
        try
        {
            stmt = conn.createStatement();
            lbArrayList = new ArrayList();
            DocLBInfo docLBInfo = new DocLBInfo();
            sSQL = " select ID,LBMC,FID,LAYER,LBBH from OA_WDLB where 1=1 and " + sCond + " order by SORT ";
            for(rs = stmt.executeQuery(sSQL); rs.next(); lbArrayList.add(docLBInfo))
            {
                docLBInfo = new DocLBInfo();
                docLBInfo.setID(rs.getInt("ID"));
                docLBInfo.setLBMC(format.toSpaceStr(rs.getString("LBMC")));
                docLBInfo.setFID(rs.getInt("FID"));
                docLBInfo.setLAYER(rs.getInt("LAYER"));
                docLBInfo.setLBBH(rs.getString("LBBH"));
            }

            rs.close();
            stmt.close();
        }
        catch(Exception e)
        {
            System.err.println(e.getMessage());
            throw e;
        }
        return lbArrayList;
    }

    public DocLBInfo getDocLBInfo(int iLBID)
        throws Exception
    {
        try
        {
            stmt = conn.createStatement();
            docLBInfo = null;
            sSQL = "select ID,LBMC,FID,LAYER,LBBH from OA_WDLB where ID=" + iLBID;
            rs = stmt.executeQuery(sSQL);
            if(rs.next())
            {
                docLBInfo = new DocLBInfo();
                docLBInfo.setID(rs.getInt("ID"));
                docLBInfo.setLBMC(format.toSpaceStr(rs.getString("LBMC")));
                docLBInfo.setFID(rs.getInt("FID"));
                docLBInfo.setLAYER(rs.getInt("LAYER"));
                docLBInfo.setLBBH(rs.getString("LBBH"));
            }
            rs.close();
            stmt.close();
        }
        catch(Exception e)
        {
            System.err.println(e.getMessage());
            throw e;
        }
        return docLBInfo;
    }

    public Collection getDocWDList()
        throws Exception
    {
        try
        {
            stmt = conn.createStatement();
            wdArrayList = new ArrayList();
            DocWDInfo docWDInfo = new DocWDInfo();
            sSQL = "select ID,LBID,WDBT,WDNR,WDRQ from OA_WDXX ";
            for(rs = stmt.executeQuery(sSQL); rs.next(); wdArrayList.add(docWDInfo))
            {
                docWDInfo = new DocWDInfo();
                docWDInfo.setID(rs.getInt("ID"));
                docWDInfo.setLBID(rs.getInt("LBID"));
                docWDInfo.setWDBT(format.toSpaceStr(rs.getString("WDBT")));
                docWDInfo.setWDNR(format.toSpaceStr(rs.getString("WDNR")));
                docWDInfo.setWDRQ(format.toSpaceStr(rs.getString("WDRQ")));
            }

            rs.close();
            stmt.close();
        }
        catch(Exception e)
        {
            System.err.println(e.getMessage());
            throw e;
        }
        return wdArrayList;
    }

    public String getAllParentID(int iLBID)
    {
        String strParentId = "0";
        int iLAYER = 0;
        try
        {
            docLBInfo = getDocLBInfo(iLBID);
            if(docLBInfo != null)
            {
                iLAYER = docLBInfo.getLAYER();
                for(int i = 0; i < iLAYER; i++)
                {
                    iLBID = docLBInfo.getFID();
                    strParentId = strParentId + "," + docLBInfo.getFID();
                    docLBInfo = getDocLBInfo(iLBID);
                }

            }
        }
        catch(Exception exception) { }
        return strParentId;
    }

    public String getReadLBID(int iYGID, String sZWID)
    {
        String strLBID = "0";
        try
        {
            sSQL = " select KZID from OA_QXYG where  KZBZ = 0 and QXID = 2 AND (         (YGID = " + iYGID + " AND ZWBZ=0)    " + "    OR (YGID in (" + sZWID + ") AND ZWBZ=1) " + " ) ";
            stmt = conn.createStatement();
            for(rs = stmt.executeQuery(sSQL); rs.next();)
                strLBID = strLBID + "," + rs.getString(1);

            rs.close();
            stmt.close();
        }
        catch(SQLException sqlexception) { }
        return strLBID;
    }

    public String getReadLBID(int iYGID, String sZWID, int iBMID)
    {
        String strLBID = "0";
        if(!strVERSION_F)
        {
            strVERSION_F = CheckPartner.isSameVersion(strVERSION_M);
            if(!strVERSION_F)
            {
                System.out.println("Version Error===" + strVERSION_M.substring(0, 15));
                return "0";
            }
        }
        try
        {
            sSQL = " select KZID from OA_QXYG where  KZBZ = 0 and QXID = 2 AND (         (YGID = " + iYGID + " AND ZWBZ=0)    " + "    OR (YGID in (" + sZWID + ") AND ZWBZ=1) " + "    OR (YGID = " + iBMID + " AND ZWBZ=2)    " + " ) ";
            stmt = conn.createStatement();
            for(rs = stmt.executeQuery(sSQL); rs.next();)
                strLBID = strLBID + "," + rs.getString(1);

            rs.close();
            stmt.close();
        }
        catch(SQLException sqlexception) { }
        return strLBID;
    }

    public String getOperateLBID(int iYGID, String sZWID, int iBMID, int iQXID)
    {
        String strLBID = "0";
        try
        {
            sSQL = " select KZID from OA_QXYG where  KZBZ = 0 and QXID = " + iQXID + " AND (  " + "       (YGID = " + iYGID + " AND ZWBZ=0)    " + "    OR (YGID in (" + sZWID + ") AND ZWBZ=1) " + "    OR (YGID = " + iBMID + " AND ZWBZ=2) " + " ) ";
            stmt = conn.createStatement();
            for(rs = stmt.executeQuery(sSQL); rs.next();)
                strLBID = strLBID + "," + rs.getString(1);

            rs.close();
            stmt.close();
        }
        catch(SQLException sqlexception) { }
        return strLBID;
    }

    public String getReadXXID(int iYGID, String sZWID)
    {
        String strLBID = "0";
        try
        {
            sSQL = " select KZID from OA_QXYG where  KZBZ = 1 and QXID = 2 AND (         (YGID = " + iYGID + " AND ZWBZ=0)    " + "    OR (YGID in (" + sZWID + ") AND ZWBZ=1) " + " ) ";
            stmt = conn.createStatement();
            for(rs = stmt.executeQuery(sSQL); rs.next();)
                strLBID = strLBID + "," + rs.getString(1);

            rs.close();
            stmt.close();
        }
        catch(SQLException sqlexception) { }
        return strLBID;
    }

    public String getReadXXID(int iYGID, String sZWID, int iBMID)
    {
        String strLBID = "0";
        try
        {
            sSQL = " select KZID from OA_QXYG where  KZBZ = 1 and QXID = 2 AND (         (YGID = " + iYGID + " AND ZWBZ=0)    " + "    OR (YGID in (" + sZWID + ") AND ZWBZ=1) " + "    OR (YGID = " + iBMID + " AND ZWBZ=2) " + " ) ";
            stmt = conn.createStatement();
            for(rs = stmt.executeQuery(sSQL); rs.next();)
                strLBID = strLBID + "," + rs.getString(1);

            rs.close();
            stmt.close();
        }
        catch(SQLException sqlexception) { }
        return strLBID;
    }

    public static void main(String args[])
    {
        DocProcess dp = new DocProcess();
        System.out.println("==" + dp.getAllParentID(11));
    }

}
